// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "Widgets/GameSettingListEntry.h"

#include "LyraSettingsListEntrySetting_KeyboardInput.generated.h"

class UKeyAlreadyBoundWarning;

class UGameSetting;
class UGameSettingPressAnyKey;
class ULyraButtonBase;
class ULyraSettingKeyboardInput;
class UObject;

//////////////////////////////////////////////////////////////////////////
// ULyraSettingsListEntrySetting_KeyboardInput
//////////////////////////////////////////////////////////////////////////

UCLASS(Abstract, Blueprintable, meta = (Category = "Settings", DisableNativeTick))
class ULyraSettingsListEntrySetting_KeyboardInput : public UGameSettingListEntry_Setting
{
	GENERATED_BODY()

public:
	/**
	 * 设置此条目所代表的具体游戏设置
	 * @param InSetting 要设置的游戏设置对象
	 */
	virtual void SetSetting(UGameSetting* InSetting) override;

protected:
	/**
	 * 当控件在原生端初始化时调用
	 */
	virtual void NativeOnInitialized() override;
	/**
	 * 当列表条目被释放时调用
	 */
	virtual void NativeOnEntryReleased() override;
	/**
	 * 当设置发生变化时调用
	 */
	virtual void OnSettingChanged() override;

	/**
	 * 处理主键按钮点击事件
	 */
	void HandlePrimaryKeyClicked();
	/**
	 * 处理副键按钮点击事件
	 */
	void HandleSecondaryKeyClicked();
	/**
	 * 处理清除按钮点击事件
	 */
	void HandleClearClicked();
	/**
	 * 处理重置为默认值按钮点击事件
	 */
	void HandleResetToDefaultClicked();

	/**
	 * 处理主键选择事件
	 * @param InKey 选择的键
	 * @param PressAnyKeyPanel 按下任意键面板
	 */
	void HandlePrimaryKeySelected(FKey InKey, UGameSettingPressAnyKey* PressAnyKeyPanel);
	/**
	 * 处理副键选择事件
	 * @param InKey 选择的键
	 * @param PressAnyKeyPanel 按下任意键面板
	 */
	void HandleSecondaryKeySelected(FKey InKey, UGameSettingPressAnyKey* PressAnyKeyPanel);
	/**
	 * 处理主键重复键选择事件（在键已绑定时）
	 * @param InKey 选择的键
	 * @param DuplicateKeyPressAnyKeyPanel 重复键警告面板
	 */
	void HandlePrimaryDuplicateKeySelected(FKey InKey, UKeyAlreadyBoundWarning* DuplicateKeyPressAnyKeyPanel) const;
	/**
	 * 处理副键重复键选择事件（在键已绑定时）
	 * @param InKey 选择的键
	 * @param DuplicateKeyPressAnyKeyPanel 重复键警告面板
	 */
	void HandleSecondaryDuplicateKeySelected(FKey InKey, UKeyAlreadyBoundWarning* DuplicateKeyPressAnyKeyPanel) const;
	/**
	 * 更改键位绑定
	 * @param BindSlot 绑定槽位
	 * @param InKey 要绑定的键
	 */
	void ChangeBinding(int32 BindSlot, FKey InKey);
	/**
	 * 处理键选择取消事件（针对按下任意键面板）
	 * @param PressAnyKeyPanel 按下任意键面板
	 */
	void HandleKeySelectionCanceled(UGameSettingPressAnyKey* PressAnyKeyPanel);
	/**
	 * 处理键选择取消事件（针对重复键警告面板）
	 * @param PressAnyKeyPanel 重复键警告面板
	 */
	void HandleKeySelectionCanceled(UKeyAlreadyBoundWarning* PressAnyKeyPanel);

	/**
	 * 刷新控件显示
	 */
	void Refresh();

private:
	UPROPERTY(Transient)
	FKey OriginalKeyToBind = EKeys::Invalid; // 记录原始要绑定的键，用于处理重复绑定情况

protected:
	UPROPERTY()
	TObjectPtr<ULyraSettingKeyboardInput> KeyboardInputSetting; // 指向键盘输入设置的指针

	UPROPERTY(EditDefaultsOnly)
	TSubclassOf<UGameSettingPressAnyKey> PressAnyKeyPanelClass; // 按下任意键面板的类引用

	UPROPERTY(EditDefaultsOnly)
	TSubclassOf<UKeyAlreadyBoundWarning> KeyAlreadyBoundWarningPanelClass; // 键已绑定警告面板的类引用

private:	// 绑定的控件
	UPROPERTY(BlueprintReadOnly, meta = (BindWidget, BlueprintProtected = true, AllowPrivateAccess = true))
	TObjectPtr<ULyraButtonBase> Button_PrimaryKey; // 主键按钮

	UPROPERTY(BlueprintReadOnly, meta = (BindWidget, BlueprintProtected = true, AllowPrivateAccess = true))
	TObjectPtr<ULyraButtonBase> Button_SecondaryKey; // 副键按钮

	UPROPERTY(BlueprintReadOnly, meta = (BindWidget, BlueprintProtected = true, AllowPrivateAccess = true))
	TObjectPtr<ULyraButtonBase> Button_Clear; // 清除按钮

	UPROPERTY(BlueprintReadOnly, meta = (BindWidget, BlueprintProtected = true, AllowPrivateAccess = true))
	TObjectPtr<ULyraButtonBase> Button_ResetToDefault; // 重置为默认值按钮
};